package chapter02;

import java.util.logging.Logger;

public class MaxContigousSubquence {
    /**
     * Cubic maximum contiguous subsequence sum algorithm.
     * @param a
     * @return
     */
    public static int maxSubSum1(int[] a) {
        int maxSnum = 0;

        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a.length; j++) {
                int thisSnum = 0;
                for (int k = i; k <= j; k++) {
                    thisSnum +=a[k];
                }
                if (thisSnum > maxSnum) {
                    maxSnum = thisSnum;
                }
            }
        }
        return maxSnum;
    }

    /**
     * Quadracit maxinum contiguous subsequence sum algorithm.
     * @param a
     * @return
     */
    public static int maxSubSum2(int [] a) {
        int maxSum = 0;
        for (int i = 0; i < a.length; i++) {
            int thisSum = 0;
            for (int j = i; j < a.length; j++) {
                thisSum += a[j];
                if (thisSum > maxSum) {
                    maxSum = thisSum;
                }
            }
        }
        return maxSum;
    }


    private static int maxSumRec(int[] a, int left,int right) {
        //基线情况
        if (left == right) {
            if (a[left] > 0) {
                return a[left];
            }else {
                return 0;
            }
        }
        int center = (left + right) / 2;

        //前半部分的最大子序列之和
        int maxLeftSum = maxSumRec(a, left, center);
        //后半部分的最大子序列之和
        int maxRightSum = maxSumRec(a, center + 1, right);
        //前半部分(包含前半部分最后一个元素)的最大子序列之和
        int maxLeftBorderSum =0, leftBorderSum = 0;
        for (int i = center; i >= left; i--) {
            leftBorderSum +=a[i];
            if (leftBorderSum > maxLeftBorderSum) {
                maxLeftBorderSum = leftBorderSum;
            }
        }
        //后半部分(包含后半部分最后一个元素)的最大子序列之和
        int maxRightBorderSum = 0, rightBorderSum = 0;
        for (int i = center + 1; i <= right; i++) {
            rightBorderSum +=a[i];
            if (rightBorderSum > maxRightBorderSum) {
                maxRightBorderSum = rightBorderSum;
            }
        }

        return max3(maxLeftSum, maxRightSum, maxLeftBorderSum + maxRightBorderSum);
    }

    private static int max3(int a, int b, int c) {
        return Math.max(c, Math.max(a, b));
    }

    public static int maxSubSum3(int[] a) {
        return maxSumRec(a, 0, a.length - 1);
    }

    /**
     * Linear-time maximum contiguous subsequence sum algorithm.
     * @param a
     * @return
     */
    public static int maxSubSum4(int[] a) {
        int maxSum = 0, thisSum = 0;

        for (int i = 0; i < a.length; i++) {
            thisSum +=a[i];
            if (thisSum > maxSum) {
                maxSum = thisSum;
            }else if (thisSum < 0) {
                thisSum = 0;
            }
        }

        return maxSum;
    }










    public static void main(String[] args) {
        int[] a =  new int[20000000];
        for (int i = 0; i < a.length; i++) {
            double random = Math.random();
            a[i] = (int) (20000000 * random) + 1;
        }
        long start,end;

        /*start = System.currentTimeMillis();
        System.out.println(maxSubSum1(a));
        end = System.currentTimeMillis();
        System.out.println(end - start);
*/
        start = System.currentTimeMillis();
        System.out.println(maxSubSum2(a));
        end = System.currentTimeMillis();
        System.out.println(end - start);

        start = System.currentTimeMillis();
        System.out.println(maxSubSum3(a));
        end = System.currentTimeMillis();
        System.out.println(end - start);

        /*start = System.currentTimeMillis();
        System.out.println(maxSubSum4(a));
        end = System.currentTimeMillis();
        System.out.println(end - start);*/


    }
}
